package com.mobapping;

import java.io.File;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class KameraItemizedOverlay  extends ItemizedOverlay {
	
	
	Context mContext;
	ImageView iv;
	int n=0;
	private IDataAdapter  webAdapter;
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);	    
		populate();
	}

	public void clearOverlay(OverlayItem overlay) {
		mOverlays.clear();	    
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public KameraItemizedOverlay(Drawable defaultMarker, Context context) {
		//super(defaultMarker);
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	@Override
	protected boolean onTap(int index) {
		
		final Timer timer = new Timer();
		final KameraOverlayItem item = (KameraOverlayItem)mOverlays.get(index);
		final Dialog dialog = new Dialog(mContext);
		webAdapter = new DataAdapter(mContext);
		webAdapter.getPictures(190, 3);
		dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.detailed_item_dialog);
		iv = (ImageView) dialog.findViewById(R.id.camera_image_view);
		int delay = 0; // delay for 5 sec.

		int period = 500; // repeat every sec.
		final Integer[] Icons = {
		    R.drawable.eczane_s,
		    R.drawable.eczane3,
		    R.drawable.icon37,
		};
		


		
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
	
				System.out.println("done");
				iv.post(new Runnable() {
			        public void run() {
			        	//iv.setImageResource(Icons[n]);
			        	File dir = mContext.getFilesDir();
			        	iv.setImageDrawable(Drawable.createFromPath(dir.getAbsolutePath() + "/" + n + ".jpg"));
			        	n++;
			        	if(n==3)
			        		n=0;
			        }
			      });
			}

		}, delay, period);

		TextView nameView = (TextView)dialog.findViewById(R.id.name);
		nameView.setText(item.getName());
	
		Button positiveButton = (Button) dialog.findViewById(R.id.positiveButton);
			
		positiveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.cancel();
				timer.cancel();
			}
		});



		dialog.show();

		return true;
	}

	private TimerTask change_image() {
		// TODO Auto-generated method stub
		iv.setImageResource(R.drawable.greenmarkerr);
		return null;
	}

	public void onClick(AlertDialog.Builder dialog) {
		//	dialog.dismiss(); 
	}


}
